﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：PWDChange.ascx.cs
//
//文件功能描述：员工修改密码
// 
//创建标识：2007.11.22 施欢
//创建描述：创建页面逻辑
//
//修改标识: 2007.11.28 施欢
//修改描述:按编码规范对代码进行了调整
//
//修改标识: 2007.12.4 施欢
//修改描述: 改正了"取消"按钮也要验证的错误 
------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Security.Cryptography;

/// <summary>
/// 员工修改自己的密码
/// </summary>
public partial class UserControls_PWDChange : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    
    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="sender">发送该事件的对象</param>
    /// <param name="e">网页事件的基本参数</param>
    protected void ibtnOk_Click(object sender, ImageClickEventArgs e)
    {

        if (Page.IsValid) //页面通过客户端验证
        {
            string strOldPassword = txtOldPwd.Text.Trim();         //获取旧密码
            byte[] bytOldPassword = EncodPassword(strOldPassword); //加密旧密码
            string strNewPassword = txtNewPwd.Text.Trim();         //获取新密码
            byte[] bytNewPassword = EncodPassword(strNewPassword); //加密新密码
            string strAccount = Session["Account"].ToString();     //获取员工登录帐号
            bool blnResult = false; //修改密码结果,成功时为true,失败时为false
            localhost.StaffService service = new localhost.StaffService();
            
            //修改密码
            blnResult = service.ChangePWD(bytOldPassword, bytNewPassword, strAccount);
            
            //显示修改密码结果
            if (blnResult == true)
            {
                Page.RegisterStartupScript("warnings", "<script>alert('修改密码成功!');</script>");
            }//blnResult==true
            
            else
            {
                Page.RegisterStartupScript("warnings", "<script>alert('修改密码失败!');</script>");
            }// blnResult==false

        }
    }
    
    /// <summary>
    /// 取消修改密码
    /// </summary>
    /// <param name="sender">发送该事件的对象</param>
    /// <param name="e">网页事件的基本参数</param>
    protected void ibtnCancel_Click(object sender, ImageClickEventArgs e)
    {
        //清空输入框
        txtOldPwd.Text = string.Empty;
        txtNewPwd.Text = string.Empty;
        txtConPwd.Text = string.Empty;
      
    }

    /// <summary>
    /// 加密输入的密码
    /// </summary>
    /// <param name="strPassword">输入的密码</param>
    /// <returns>加密后的密码</returns>
    private byte[] EncodPassword(string strPassword)
    {
        ASCIIEncoding AE = new ASCIIEncoding();

        //将输入的字符串密码转化成字节数组
        byte[] arSource = AE.GetBytes(strPassword); 
        SHA1 SHhash = new SHA1Managed();
        byte[] arHashValue;

        //生成密码的哈希值
        arHashValue = SHhash.ComputeHash(arSource); 
        
        return arHashValue;
    }
}
